/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.springBoot.springBootSysCore.modules.entity.system;

import java.util.Date;
import java.util.Map;

import javax.persistence.*;

import com.springBoot.springBootSysCore.common.comment.annotation.MyComment;
import com.springBoot.springBootSysCore.common.utils.encoding.Encodes;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;

import com.springBoot.springBootSysCore.common.persistence.DataEntity;
import com.springBoot.springBootSysCore.common.utils.StringUtils;

/**
 * 日志Entity
 * @author ThinkGem
 * @version 2014-8-19
 */
@Table(name="SYS_LOG")
@Entity
@MyComment("日志表")
public class SysLog extends DataEntity<SysLog> {

	private static final long serialVersionUID = 1L;
	private String type; 		// 日志类型（1：接入日志；2：错误日志）
	private String title;		// 日志标题
	private String remoteAddr; 	// 操作用户的IP地址
	private String requestUri; 	// 操作的URI
	private String method; 		// 操作的方式
	private String params; 		// 操作提交的数据
	private String userAgent;	// 操作用户代理信息
	private String exception; 	// 异常信息
	private String respos;//响应数据

	
	// 日志类型（1：接入日志；2：错误日志）
	public static final String TYPE_ACCESS = "1";
	public static final String TYPE_EXCEPTION = "2";
	
	public SysLog(){
		super();
	}
	
	public SysLog(String id){
		super(id);
	}

	@Column(name="TYPE")
	@MyComment("日志类型")
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
	@Column(name="TITLE")
	@MyComment("日志标题")
	@Lob
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}
	@Column(name="REMOTE_ADDR")
	@MyComment("操作用户的IP地址")
	public String getRemoteAddr() {
		return remoteAddr;
	}

	public void setRemoteAddr(String remoteAddr) {
		this.remoteAddr = remoteAddr;
	}
	@Column(name="USER_AGENT")
	@MyComment("操作用户代理信息")
	public String getUserAgent() {
		return userAgent;
	}

	public void setUserAgent(String userAgent) {
		this.userAgent = userAgent;
	}
	@Column(name="REQUEST_URL")
	@MyComment("操作的URI")
	@Lob
	public String getRequestUri() {
		return requestUri;
	}

	public void setRequestUri(String requestUri) {
		this.requestUri = requestUri;
	}
	@Column(name="METHOD")
	@MyComment("操作的方式")
	public String getMethod() {
		return method;
	}

	public void setMethod(String method) {
		this.method = method;
	}
	@Column(name="PARAMS")
	@MyComment("操作提交的数据")
	@Lob
	public String getParams() {
		return params;
	}

	public void setParams(String params) {
		this.params = params;
	}
	@Column(name="EXCEPTION")
	@MyComment("异常信息")
	@Lob
	public String getException() {
		return Encodes.escapeHtml(exception);
	}

	public void setException(String exception) {
		this.exception = exception;
	}

	/**
	 * 设置请求参数
	 * @param paramMap
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void setParams(Map paramMap){
		if (paramMap == null){
			return;
		}
		StringBuilder params = new StringBuilder();
		for (Map.Entry<String, String[]> param : ((Map<String, String[]>)paramMap).entrySet()){
			params.append(("".equals(params.toString()) ? "" : "&") + param.getKey() + "=");
			String paramValue = (param.getValue() != null && param.getValue().length > 0 ? param.getValue()[0] : "");
			params.append(StringUtils.abbr(StringUtils.endsWithIgnoreCase(param.getKey(), "password") ? "" : paramValue, 100));
		}
		this.params = params.toString();
	}
	
	@Override
	public String toString() {
		return ReflectionToStringBuilder.toString(this);
	}

	@Lob
	public String getRespos() {
		return respos;
	}

	public void setRespos(String respos) {
		this.respos = respos;
	}
}